package com.crazymaker.springcloud.standard.redis;

import com.crazymaker.springcloud.common.context.SessionHolder;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.stereotype.Component;

@Slf4j
@Component
public class RedisRepository {

    @Autowired
    private RedisTemplate redisTemplate;

    /**
     * hash取值
     * @param key
     *  hash名字
     * @param hkey
     *  hash key
     * @return
     */
    public String hget(String key, String hkey) {
        return (String) redisTemplate.opsForHash().get(key, hkey);
    }

    public void hset(String key, String hkey, String value) {
        redisTemplate.opsForHash().put(key, hkey, value);
    }

    /**
     * 获取UID对应的SessionID
     * @param uid
     * @return
     */
    public String getSessionId(String uid) {
        return hget("SESSION:ID:" + SessionHolder.getSessionIDStore(), uid);
    }

    /**
     * uid和sessionId的关系保存
     * @param uid
     * @param sessionId
     */
    public void setSessionId(String uid, String sessionId) {
        hset("SESSION:ID:" + SessionHolder.getSessionIDStore(), uid, sessionId);
    }
}
